<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="common/include :: header('服务项列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<!--模块一,用于数据的高级查询-->
			<div class="col-sm-12 search-collapse">
				<form id="serviceItem-form">
					<div class="select-list">
						<ul>
							<li>
								名称：<input type="text" name="name"/>
							</li>
							<li>
								是否套餐：
								<select name="carPackageStatus" th:with="type=${@dict.getType('si_car_package')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								服务分类：
								<select name="serviceCatalogStatus" th:with="type=${@dict.getType('si_service_catalog')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								审核状态：
								<select name="auditStatus" th:with="type=${@dict.getType('si_audit_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								上架状态：
								<select name="saleStatus" th:with="type=${@dict.getType('si_sale_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

			<!--模块二,新增,修改,删除,导出的四个按钮-->
			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:serviceItem:add">
	                <i class="fa fa-plus"></i> 新增
	            </a>
				<a class="btn btn-warning disabled" id="startAudit" onclick="startAudit()" shiro:hasPermission="business:serviceItem:add">
					<i class="fa fa-plus"></i> 发起审核
				</a>
	        </div>

			<!--表格模块,使用了异步刷新的前端框架bootstrap-table-->
	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="common/include :: footer" />
	<script th:inline="javascript">
		var carPackageDates = [[${@dict.getType('si_car_package')}]];
		var serviceCatalogDates = [[${@dict.getType('si_service_catalog')}]];
		var auditStatusDates = [[${@dict.getType('si_audit_status')}]];
		var saleStatusDates = [[${@dict.getType('si_sale_status')}]];
		var prefix = ctx + "/business/serviceItem";
		$(function() {
		    var options = {
		        url: prefix + "/query",						//查询的url路径
		        createUrl: prefix + "/addPage",				//新增的url
		        updateUrl: prefix + "/editPage?id={id}",	//编辑的url 携带了id
		        exportUrl: prefix + "/export",				//导入的url
				sortName: "seq",
				modalName: "服务项",
		        columns: [{
		            checkbox: true						//在表格前面加入了复选框
		        },
		        {
		            field: 'id',
		            title: '服务项id',
					visible: false						//设为false表示隐藏id列,在页面上不做显示
		        },
		        {
		            field: 'name',
		            title: '服务项名称',
		            sortable: true
		        },
				{
					field: 'originalPrice',
					title: '服务项原价',
					sortable: true
				},
				{
					field: 'discountPrice',
					title: '服务项折扣价',
					sortable: true
				},
				{
					field: 'carPackage',
					title: '是否套餐',
					sortable: true,
					formatter: function(value, row, index) {
						/*console.log(value, carPackageDates);
						return $.table.selectDictLabel(carPackageDates, value);*/
						if (value) {
							return "是";
						} else {
							return "否";
						}
					}
				},

				{
					field: 'info',
					title: '备注信息',
					sortable: true,
					formatter: function(value, row, index) {
						return $.table.tooltip(value, 5, "open");
					}
				},
				{
					field: 'serviceCatalog',
					title: '服务分类',
					sortable: true,
					formatter: function(value, row, index) {
						return $.table.selectDictLabel(serviceCatalogDates, value);
					}
				},
				{
					field: 'auditStatus',
					title: '审核状态',
					sortable: true,
					formatter: function(value, row, index) {
						return $.table.selectDictLabel(auditStatusDates, value);
					}
				}, {
					field: 'saleStatus',
					title: '上架状态',
					sortable: true,
						formatter: function(value, row, index) {
							return $.table.selectDictLabel(saleStatusDates, value);
						}
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		                var actions = [];
		                var saleClass="";
		                var editClass="";
		                //如果是上架状态或者是审核中的状态设置不允许编辑
		                if (row.saleStatus == 1 || row.auditStatus == 1) {
							editClass='disabled';
                        }

		                actions.push('<a class="btn btn-success btn-xs '+ editClass +'" href="javascript:void(0)" ' +
								'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
		                //判断是否为上架状态,如果为下架状态就显示上架按钮
		                if (row.saleStatus == 0) {
		                	// 如果是套餐,并且审核状态不是审核通过的话就给他设置不允许上架
		                	if (row.carPackage == 1 && row.auditStatus != 2) {
								saleClass='disabled';
                            }
							actions.push('<a class="btn btn-info btn-xs '+saleClass+'" href="javascript:void(0)"' +
									' onclick="saleOn(\'' + row.id + '\')"><i class="fa fa-remove"></i>上架</a>');
		                	//如果是上架状态就显示下架的按钮
                        } else if (row.saleStatus == 1){
							actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)"' +
									' onclick="saleOff(\'' + row.id + '\')"><i class="fa fa-remove"></i>下架</a>');
                        }
		                return actions.join(' ');
		            }
		        }]
		    };
		    $.table.init(options);
		});


		$("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
			//获取选中的数据
			var rows = $('#bootstrap-table').bootstrapTable("getSelections");
			var data = rows[0];
			//判断如果个数大于1或者长度等于0就禁止点击
			if (rows.length > 1 || rows.length == 0) {
				$("#startAudit").addClass("disabled");
				return;
			}
			//判断是否是套餐
			if (data.carPackage == 0) {
				$("#startAudit").addClass("disabled");
				return;
			}

			//判断状态,如果是审核中和审核通过不能点击
			if (data.auditStatus == 1 || data.auditStatus == 2) {
				$("#startAudit").addClass("disabled");
				return;
			}

			$("#startAudit").removeClass("disabled");
		});

		function startAudit() {
			//获取选中的数据
			var rows = $('#bootstrap-table').bootstrapTable("getSelections");
			var row = rows[0];
			$.modal.open("发起审核流程","/business/serviceItem/startAuditPage?id=" + row.id);
		}

		function saleOn(id) {
			$.modal.confirm("确定要上架吗", function() {
				$.operate.get('/business/serviceItem/saleOn?id='+ id);
			});
		}
		function saleOff(id) {
			$.modal.confirm("确定要下架吗", function() {
				$.operate.get('/business/serviceItem/saleOff?id='+ id);
			});
		}

	</script>
</body>
</html>